package com.xing.dao;

import static org.junit.Assert.fail;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BaseDaoImplTest {

	private static BaseDao analysesDao;

	@Before
	public void setUp() throws Exception {
		try {
			ClassPathXmlApplicationContext cc = new ClassPathXmlApplicationContext(
					"bean.xml");
			analysesDao = (BaseDao) cc.getBean("analysesDao");
			System.out.println("---init AnalysesDao from spring context!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testGetListString() {
		List<?> list = analysesDao.getList("from Test");
		System.out.println(list);
	}

	@Test
	public void testGetListStringStringStringListOfParamModel() {
		String fetchParam = "";
		String hql = "from Test where intId < :intId ";
		String orderBy = " order by intId desc";
		
		List<ParamModel> paramModelList = new ArrayList<ParamModel>();
		paramModelList.add(new ParamModel("intId", 8));
		
		List<?> list = analysesDao.getList(fetchParam, hql, orderBy, paramModelList);
		System.out.println(list);
	}

	@Test
	public void testGetListByNameQuery() {
		List<ParamModel> paramModelList = new ArrayList<ParamModel>();
		paramModelList.add(new ParamModel("intId", 8));
		
		List<?> list = analysesDao.getListByNameQuery("getTestList", paramModelList);
		System.out.println(((Object[])list.get(0))[1]);
	}

	@Test
	public void testExecuteByNameQuery() {
		List<ParamModel> paramModelList = new ArrayList<ParamModel>();
		paramModelList.add(new ParamModel("intId", 8));
		
		List<?> list = analysesDao.getListByNameQuery("getTestList", paramModelList);
		System.out.println(((Object[])list.get(0))[1]);
		
		paramModelList.clear();
		paramModelList.add(new ParamModel("intId", 8));
		paramModelList.add(new ParamModel("value", "testtest"));
		analysesDao.executeByNameQuery("updateTest", paramModelList);
			
		paramModelList.clear();
		paramModelList.add(new ParamModel("intId", 8));
		list = analysesDao.getListByNameQuery("getTestList", paramModelList);
		System.out.println(((Object[])list.get(0))[1]);
	}

	@Test
	public void testGetDataByCriteria() {
		Class<?> clazz = com.xing.model.test.Test.class;
		List<Criterion> criList = new ArrayList<Criterion>();
		criList.add(Restrictions.le("intId", 8));
		
		List<Order> ordList = new ArrayList<Order>();
		ordList.add(Order.desc("intId"));
		
		List<Projection> proList = new ArrayList<Projection>();
		proList.add(Projections.property("value"));
		proList.add(Projections.property("intId"));
//		Projections.groupProperty("value");
		
		List<?> list = analysesDao.getDataByCriteria(clazz, criList, ordList, proList);
		System.out.println(list);
	}

	@Test
	public void testCallProcedureWithoutResult() {
		fail("Not yet implemented");
	}

	@Test
	public void testGetListBySql() {
		
		String sql = "select * from test where int_id < ?";
		Object[] args = new Object[1];
		args[0] = "3";
		
		int[] argTypes = new int[1];
		argTypes[0] = Types.INTEGER;
		
		List<?> list = analysesDao.getListBySql(sql, args, argTypes);
		System.out.println(list);
	}

	@Test
	public void testSaveList() {
		
		List<com.xing.model.test.Test> list = new ArrayList<com.xing.model.test.Test>();
		for (int i = 0; i < 10; i++) {
			com.xing.model.test.Test tmp = new com.xing.model.test.Test();
			tmp.setValue("--" + i);
			list.add(tmp);
		}
		
		analysesDao.saveList(list);
	}

	@Test
	public void testUpdateByHqlString() {
		
		String hql = "update Test set value = 'testUpdateHql' where intId = 1";
		
		analysesDao.updateByHql(hql);
	}

	@Test
	public void testUpdateByHqlStringListOfParamModel() {

		String hql = "update Test set value = :value where intId = :intId";
		List<ParamModel> paramModelList = new ArrayList<ParamModel>();
		paramModelList.add(new ParamModel("intId", 8));
		paramModelList.add(new ParamModel("value", "testUpdateHql"));
		
		analysesDao.updateByHql(hql, paramModelList);
	}

}
